package com.vegetable.modules.controller.outbound;


import com.alibaba.fastjson.JSON;
import com.vegetable.common.annotation.PreventDuplicateSubmit;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.ObjectUtil;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.outbound.SalesProxy;
import com.vegetable.modules.service.outbound.ISalesProxyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 代销发货 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-04-02
 */
@Slf4j
@RestController
@RequestMapping("/outStorage/salesProxy")
@AllArgsConstructor
@Api(value = "代销发货", tags = "代销发货")
public class SalesProxyController extends AbstractController {

    private ISalesProxyService salesProxyService;
    /**
     * 所有代销发货列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询代销发货列表", notes = "条件查询代销发货列表")
    public R list(@RequestParam Map<String, Object> params){
        log.info("条件查询代销发货列表{}", JSON.toJSONString(params));
        PageUtils page = salesProxyService.queryPage(params);
        return R.data( page);
    }

    /**
     * 详情代销发货
     */
    @SysLog(value = "详情代销发货")
    @GetMapping("/detailSalesProxy")
    @ApiOperation(value = "根据id查询详情代销发货", notes = "根据id查询详情代销发货")
    public R detailSalesProxy(Long id){
        if (id == null || id == -1){
            return R.fail("订单主键不能为空");
        }
        log.info("根据id查询详情代销发货：{}", JSON.toJSONString(id));
        try {
            return salesProxyService.detailSalesProxy(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 新增代销发货
     */
    @SysLog(value = "新增代销发货")
    @PostMapping("/saveSalesProxy")
    @ApiOperation(value = "新增代销发货", notes = "新增代销发货")
    public R saveSalesProxy(@RequestBody SalesProxy salesProxy){
        log.info("新增代销发货：{}", JSON.toJSONString(salesProxy));
        salesProxy.setCreateUserId(getUserId());
        try {
            return salesProxyService.saveSalesProxy(salesProxy);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 提交代销发货
     */
    @SysLog(value = "提交代销发货")
    @PostMapping("/submitSalesProxy")
    @PreventDuplicateSubmit(expireTime = 5)
    @ApiOperation(value = "提交代销发货", notes = "修改代销发货")
    public R submitSalesProxy(@RequestBody SalesProxy salesProxy){
        if (salesProxy.getId() == null || ObjectUtil.isEmpty(salesProxy.getDocNo())){
            return R.fail("订单id和单号不能为空");
        }
        log.info("提交代销发货：{}", JSON.toJSONString(salesProxy));
        salesProxy.setCreateUserId(getUserId());
        try {
            return salesProxyService.submitSalesProxy(salesProxy);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除代销发货
     */
    @SysLog(value = "删除代销发货")
    @GetMapping("/deleteSalesProxy")
    @ApiOperation(value = "根据id删除代销发货", notes = "根据id删除代销发货")
    public R deleteSalesProxy(Long id){
        log.info("删除代销发货：{}", JSON.toJSONString(id));
        try {
            return salesProxyService.deleteSalesProxy(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }

}
